//
//  Config.m
//  NPS
//
//  Created by jadd on 15/10/16.
//  Copyright © 2015年 jadd. All rights reserved.
//

#import "Config.h"
#
@implementation Config


+(NSString *) getAppVersion{
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    return version;
}

//检查是否已经登录
+(BOOL) hasLogin
{
    NSString *oid = [[NSUserDefaults standardUserDefaults] objectForKey:UserDefault_uid];
    return (oid == nil || [oid isEqualToString:@""]);
}

+(void)logout{

    [[NSUserDefaults standardUserDefaults] removeObjectForKey:UserDefault_uid];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:UserDefault_access_key];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:UserDefault_uName];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:UserDefault_logo];
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:UserDefault_answerOnce];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:UserDefault_answerTimes];
    
    
    [MyGlobal sharedVariables].isReLogin = NO;
  
    [MyGlobal sharedVariables].storesArray = nil;
    [MyGlobal sharedVariables].token = nil;
    [MyGlobal sharedVariables].random = nil;
    
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(NSString *)getAccess_key
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:UserDefault_access_key];
    
}
+(NSString *)getUser_id
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:UserDefault_uid];
    
}

+(void) showMessage: (NSString *)string {
    [SVProgressHUD setBackgroundColor:SVProgress_Background];
    
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD setInfoImage:[UIImage imageNamed:@"111"]];
    [SVProgressHUD showInfoWithStatus:string];
    
}

+(void)showActivityView
{
    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:@"加载中..." maskType:SVProgressHUDMaskTypeNone];
}

+(void)hideActivityView
{
    [SVProgressHUD dismiss];
   }

/**
 
 *GTM 转码 base64
 
 */
+(NSString *)GTMEncodeTest:(NSString *)originStr
{
    NSString* encodeResult = nil;
    NSData* originData = [originStr dataUsingEncoding:NSUTF8StringEncoding];
    NSData* encodeData = [GTMBase64 encodeData:originData];
    encodeResult = [[NSString alloc] initWithData:encodeData encoding:NSUTF8StringEncoding];
    
    return encodeResult;
}
/**
 
 * GTM 解码
 
 */
+(NSString *)GMTDecode :(NSString *)encodeStr
{
    NSString* decodeResult = nil;
    NSData* encodeData = [encodeStr dataUsingEncoding:NSUTF8StringEncoding];
    NSData* decodeData = [GTMBase64 decodeData:encodeData];
    decodeResult = [[NSString alloc] initWithData:decodeData encoding:NSUTF8StringEncoding];
    return decodeResult;
    
}

+ (NSString *)md5:(NSString *)str
{
    if(str != nil){
        return [str md5];
    }
    else
    {
        return @"";
    }
}

+ (NSString *)generateStringToJson:(NSMutableDictionary *)params {
    //先转成json字符
    NSString *jsonStr = [params JSONString];
    //去掉首位大括号{}
    NSRange range = NSMakeRange(1, jsonStr.length - 2);
    NSString *jsonStr1 = [jsonStr substringWithRange:range];
    //用','进行分割
    NSArray *array = [jsonStr1 componentsSeparatedByString:@","];
    //对字符串进行排序
    NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    //生成新的json字符串
    NSMutableString *returnJsonStr = [[NSMutableString alloc] init];
    for (int i = 0; i < [sortedArray count]; i++) {
        NSString *key = [sortedArray objectAtIndex:i]; //从排序好的参数数组中,取得key
        [returnJsonStr appendString:key];
        //最后一个字符不加','
        if (i != sortedArray.count - 1) {
            [returnJsonStr appendString:@","];
        }
    }
    NSString *finalStr = [NSString stringWithFormat:@"{%@}",returnJsonStr];
    //返回字符串的md5值
    //finalStr = [finalStr md5];
    return finalStr;
}

@end
